<template>
	<Layout>

        <Table ref='table' :height="tableHeight"  no-data-text="没有查询到相关数据!"
                border :columns="columns1"  :data="dataTable">
        </Table>

        <Row style="margin-right:10px;">
            <Col span="24" >
                <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize" @on-change="pageChange" @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray" show-total show-sizer show-elevator />
            </Col>
        </Row>
	</layout>
</template>

<script>
	import {Api} from '../../../../api/index.js'
	export default {
		data() {
			return {
				form1: {
				},
                //分页参数
                pageSizeArray: [10, 20, 50],
                pagedata: {
                    total: 0,
                    pageSize: 10,
                    currentPage: 1,
                },
				columns1: [
				    {
                        type: 'index',
                        title: '序号',
						width: 80,
						align: 'center',
					},
					{
                        width: 150,
                        title: '填报日期',
                        align: 'center',
                        key: 'reportDate',
                        render:(h,params)=>{
                            return h('div',[
                                (this.formatDate(new Date(params.row.reportDate), 'yyyy-MM-dd'))
                            ]);
                        }
					},
					{
                        width: 150,
						title: '作业单位',
						key: 'operateOrg',
						align: 'center'
					},
					{
					    width: 210,
						title: '作业类型',
						key: 'workType',
                        align: 'center'
					},
					{
					    width: 210,
						title: '等级',
						key: 'workGrade',
						align: 'center'
					},
					{
                        width: 150,
						title: '危险作业内容',
						key: 'operateContent',
						align: 'left'
					},
                    {
                        width: 150,
                        title: '计划开始时间',
                        key: 'planStartTime',
                        align: 'center',
                        render:(h,params)=>{
                            return h('div',[
                                (this.formatDate(new Date(params.row.planStartTime), 'yyyy-MM-dd hh:ss'))
                            ]);
                        }
                    },
                    {
                        width: 150,
                        title: '计划终止时间',
                        key: 'planEndTime',
                        align: 'left',
                        render:(h,params)=>{
                            return h('div',[
                                (this.formatDate(new Date(params.row.planEndTime), 'yyyy-MM-dd hh:ss'))
                            ]);
                        }
                    },
                    {
                        width: 100,
                        title: '上报状态',
                        key: '',
                        align: 'center'
                    },
                    {
                        width: 100,
                        title: '审核状态',
                        key: '',
                        align: 'center'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.save(params.row);
                                        }
                                    }
                                }, '选择')
                            ]);
                        }
                    }
				],
                tableHeight : 450,
                dataTable : [],
			}
		},
		mounted: function() {
            this.tableHeight = window.innerHeight*0.85 - this.$refs.table.$el.offsetTop - 73;
			this.query();
        },
		methods: {
            pageChange(pageNum) {
                this.pagedata.currentPage = pageNum;
                this.query();
            },
            pageSizeChange(pageSize) {
                this.pagedata.pageSize = pageSize;
                this.query();
            },

            copyArray(arr) {
                return JSON.parse(JSON.stringify(arr));
            },


            save(row){
                this.$emit('getSelectData',row);
            },

            query() {
                let data = {
                    currentPage: this.pagedata.currentPage,
                    size: this.pagedata.pageSize
                }

                if( this.form1.reportDate!='' && this.form1.reportDate!=undefined ){
                    this.form1.reportDate = this.formatDate(new Date(this.form1.reportDate), 'yyyy-MM-dd');
                }else{
                    delete this.form1['reportDate'];
                }
                data = Object.assign(data, JSON.parse(JSON.stringify(this.form1)));

                let url = Api.queryPTWPlan;
                this.$http.get(url, {params: data}).then((res) => {
                    this.dataTable = res.data.data;
                    this.pagedata.total = res.data.totalSize;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            reset() {
                this.form1 = {}
            },
            formatDate(date, fmt) {
                if (/(y+)/.test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
                }
                let o = {
                    'M+': date.getMonth() + 1,
                    'd+': date.getDate(),
                    'h+': date.getHours(),
                    'm+': date.getMinutes(),
                    's+': date.getSeconds()
                };
                for (let k in o) {
                    if (new RegExp(`(${k})`).test(fmt)) {
                        let str = o[k] + '';
                        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
                    }
                }
                return fmt;
            },
            padLeftZero(str) {
                return ('00' + str).substr(str.length);
            },
        },
	}
</script>
